package com.lovo.middleman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.middleman.bean.RolePurview;
import com.lovo.middleman.bean.UserBean;
import com.lovo.middleman.bean.UserRoleBean;
import com.lovo.middleman.mapper.RolePurviewMapper;
import com.lovo.middleman.mapper.UserMapper;
import com.lovo.middleman.mapper.UserRoleMapper;
import com.lovo.middleman.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserBean> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RolePurviewMapper rolePurviewMapper;

    @Override
    public Page<UserBean> queryPageBeanItem(int curPage,
                                            int pageSize,
                                            String userName) {
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        if (userName != null && !"".equals(userName)) {
            queryWrapper.like("user_name", userName);
        }
        return userMapper.selectPage(new Page<>(curPage, pageSize), queryWrapper);
    }

    @Override
    public void addUser(UserBean userBean) {
        userMapper.insert(userBean);
    }

    @Override
    public void deleteUser(int userId) {
        userMapper.deleteById(userId);
    }

//    修改用户 , 注意用户权限一般是删除整个权限 , 在重写添加权限(传参是权限数组int[] roleId)
    @Override
    public void updateUser(int userId,int[] roleId) {
        QueryWrapper<UserRoleBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserRoleBean> userRoleBeans = userRoleMapper.selectList(queryWrapper);
        for (UserRoleBean userRoleBean : userRoleBeans) {
            userRoleBean.setRoleId(0);
            if (userRoleBean.getRoleId()==0){
                for (int i : roleId) {
                    userRoleBean.setRoleId(i);
                }
            }
        }

    }

    @Override
    public UserBean login(String userName, String passWord) {
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",userName);
        queryWrapper.eq("pass_word",passWord);
        UserBean userBean =userMapper.selectOne(queryWrapper);

        QueryWrapper<UserRoleBean> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",userBean.getUserId());
        List<UserRoleBean> userRoleBean = userRoleMapper.selectList(queryWrapper1);
        List<Integer> list = new ArrayList<>();
        for (UserRoleBean roleBean : userRoleBean) {
            QueryWrapper<RolePurview> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("role_id",roleBean.getRoleId());
            List<RolePurview> rolePurview = rolePurviewMapper.selectList(queryWrapper2);
            for (RolePurview purview : rolePurview) {
                list.add(purview.getPurviewId());
            }
        }
        userBean.setPurviewList(list);
        return userBean;
    }
}
